package modeller.databasedesignmodel.servicedemand.transaction.proceduralstatement;

import modeller.databasedesignmodel.relation.Relation;
import modeller.databasedesignmodel.servicedemand.IServiceDemand;

/**
 * Created by:  Jason Ye
 * Date:        24/03/2012
 * Time:        09:30
 */
public class BranchServiceDemand implements ProceduralStatementServiceDemand {

    private IServiceDemand trueDemand;
    private IServiceDemand falseDemand;
    private double trueBranchProb;

    public BranchServiceDemand(IServiceDemand trueDemand, IServiceDemand falseDemand, double trueBranchProb) {
        this.trueDemand = trueDemand;
        this.falseDemand = falseDemand;
        this.trueBranchProb = trueBranchProb;
    }

    @Override
    public String myToString() {

        return "BRANCH Statement:\n\t" +
                "True branch: " + trueDemand.myToString() +
                "\n\tFalse branch: " + falseDemand.myToString()
                + "\n\tTrue branch execution probability = " + trueBranchProb;

    }

    public IServiceDemand getTrueDemand() {
        return trueDemand;
    }

    public void setTrueDemand(IServiceDemand trueDemand) {
        this.trueDemand = trueDemand;
    }

    public IServiceDemand getFalseDemand() {
        return falseDemand;
    }

    public void setFalseDemand(IServiceDemand falseDemand) {
        this.falseDemand = falseDemand;
    }

    public double getTrueBranchProb() {
        return trueBranchProb;
    }

    public void setTrueBranchProb(double trueBranchProb) {
        this.trueBranchProb = trueBranchProb;
    }

    @Override
    public double getServiceDemandForRelation(Relation relation) {
        double trueD = trueDemand.getServiceDemandForRelation(relation) * trueBranchProb;
        double falseD = falseDemand.getServiceDemandForRelation(relation) * (1 - trueBranchProb);
        return trueD + falseD;
    }
}
